AxoCalculator implements several programming languages. This document describes its implementation of the Basic language. It is a simplified subset of Microsoft QuickBASIC. One major omission is that statement labels and the "go to" command are not supported. All conditional branches and loops must be written using the modern block structured Basic commands :
if - then - else - end if
for - next
while - wend
do - loop until
Another non-standard feature is that AxoCalculator outputs the result of any expression which lacks an assignment operator (=). To assign values to two variables, add them and output the result use the following three lines :
a = 10
b = 20
a + b
To execute the above program, first choose "Settings…" under the "Calculator" menu, and make sure the programming language is set to Basic. Select (highlight) the above three lines using the mouse, then press the "enter" key.
Strings
Character strings are enclosed in double quotes (e.g. "This is a string"). To include a double quote, tab or return in a character string, use
\" for a double quote,
\t for a tab
\r or \n for a return.
String variable names do not need a "$" suffix. They can be created in two ways. The simplest is to assign a string to a previously unused variable name. For example,
aString = "Hello world"
As an alternative, use the "NewString" procedure.
NewString (bString)
bString="abcde"
Strings can be appended to one another using the "Concat" function.
cString = concat (aString, " ", bString)
Individual characters in a string can be accessed and manipulated. A string variable behaves like a 255 element array. Each element is one character. Accessing an element returns the ASCII code of that character. The length of the string is stored in the zero indexed element.
Comments are permitted, following a single quote ( ' ) as the first character in a line. An ampersand ( & ) as the first character of a line indicates a continuation line.
For example, select the following 6 lines then press "enter".
' Demonstrate comments, continuation lines and character strings
AxoCalculator programs can interact with the user via standard dialogs. This is done using two built in subroutine calls, "Alert" and "PoseDialog". These subroutines are described in the "Procedures and Functions" document. "Alert" is used for simple messages or for returning results. "PoseDialog" is used for requesting one or more numerical values. An example program follows :
PoseDialog ("\r Calculate the volume and surface area of a sphere",
& "Radius of sphere ",radius)
theSA = 4 * pi * radius ^ 2
theVolume = (4 / 3) * pi * radius ^ 3
Alert ("The volume and surface area of a \r sphere with radius ", radius,
& " are : \r Volume = ",theVolume,
& "\r Surface area = ",theSA)
Conditional Branch
AxoCalculator supports the standard "if then else end if" statement for conditional branching. A program demonstrating this statement follows :
' This program finds the square root of a number entered by the user
PoseDialog ("\r Find the square root of a number", "Enter the number", a)
if (a < 0) then
Alert ("Can't calculate the square root of a negative number : ",a)
else
b = sqrt (a)
Alert ("\r The square root of ",a,"is ",b)
end if
Loop Commands
AxoCalculator supports three commands for executing a group of statements multiple times. These loop commands are "For - Next", "While - Wend", and "Do - Loop Until". Executable programs demonstrating each of these commands follow.
Note : To interrupt a running program (for example to escape from
an infinite loop) press the "esc" key, or the Cmd-period
key combination.
• The "For" command
For i = 1 to 5
a = i * 5
b = sin (a)
Print "sin ( ",a,") = ",b
Next i
• The "While" command
a = 5
While (a >= 0)
b = exp (a)
Print "exp ( ",a,") = ",b
a = a - 1
Wend
• The "Do" command
a = 0
b = 0
Do
a = a + 1
b = b + 2
c = b ^ a
Print b,"^ ",a," = ",c
Loop Until a > 5
Array Variables
Array variables can be created in two ways. They can be declared using the "DIM" statement at the start of a procedure or program,
DIM anArr(50)
or they can be created anywhere using the "NewArray" procedure,
NewArray (anArray, arraySize)
Subroutines, Functions and Programs
AxoCalculator's programming language can be extended by loading user defined Subs, Functions and Programs. These all have the same basic form :
• a declaration line which includes a name and lists any parameters
• an optional local variable declaration section
• a program section terminating with an "end" statement.
Note : Parameters are always passed by value.
Variable parameters are not supported.
Programs can not have any parameters.
A Sub, Function or Program can be loaded by selecting it's text, then pressing "enter" or choosing "Load" under the "Calculator" menu. The only difference is that "Load" will not attempt to execute any lines of code outside the subroutine declaration, and may therefore produce a clearer error message.
To run a Sub, Function or Program, type in its name followed by any parameters. A Function may be executed as part of a numerical expression. Subs and Functions may call other Subs and Functions.
Declaring a Subroutine
Here is an example of how do declare a simple subroutine with a single parameter. This subroutine has no local variable declaration section. To load it, select the following 5 lines, and press "enter". To run it, type "CountTo(10)" then "enter".
Sub CountTo (Number)
For j = 1 to Number
Print j
Next j
end
Declaring a Function
Here is an example of how do declare a simple Function with a single parameter. A function returns a numerical result. Load this example function as above.
function Factorial (n)
f = 1
For j = 1 to n
f = f * j
Next j
Factorial = f
end
Here are two examples of how to use the "Factorial" function.
Factorial(10)
Print "The natural log of factorial 5 = ", ln( Factorial(5))
One practical use for functions is for unit conversion. Here is and example function which converts inches to centimeters.
function inchToCm (inch)
inchToCm = inch * 2.54
end
inchToCm(10)
Note: A general unit conversion function is provided in the document
"Unit Conversions" in the "AxoCalculator AutoLoad" folder.
Adding a Program to the Calculator Menu
Here is an example of how do declare a simple program. Note that a program has no parameter list, and its name is followed by an optional character string. If present, this string is appended to the calculator menu when the program is loaded. If the second last character of the string is a slash ( / ) then the last character becomes a command key equivalent for running the program. Load the program as above. To run it, type "CountDown" then "enter", or select "Count Down to Lift Off" from the "Calculator" menu.
program CountDown "Count Down to Lift Off/9"
Print ""
Print "Prepare for count down."
FlushOutput
' • Pause •
For k = 1 to 50
a = exp(2.0)
Next k
' • Start the Count Down •
For m = 0 to 10
j = 10 - m
if (j = 3) Print "Ignition."
if (j = 0) then
Print "Lift Off !!"
else
Print j
endif
FlushOutput
Beep
j = j - 1
' • Slow down the count •
For k = 1 to 20
a = exp(2.0)
next k
next m
end
Auto-loading a Program
A useful program can be automatically loaded every time AxoCalculator is started up. To auto-load one or more programs, simply place them in the folder "AxoCalculator AutoLoad". This folder must be located in the same folder as the AxoCalculator program.